//
// Copyright 2016 riteme
//

#ifndef RSRL_RENDERER_H_
#define RSRL_RENDERER_H_

#include "Defs.h"
#include "Math.h"
#include "Texture.h"
#include "Vertex.h"

namespace rsr {

class Renderer {
 public:
    Renderer(Texture *texture);

    void set_target(Texture *texture);
    void clear(const Color4f &c);
    void apply();

 private:
    void _map_ndc(const Vertex &v, int &x, int &y) const;
    int _get_point_code(const Vertex &v) const;

    void _rasterize_pixel(const int x, const int y, const Color4f c,
                          const Texcoord2f &tex);
    void _rasterize_pixel(const Vertex &v);
    void _rasterize_line(const Vertex &v1, const Vertex &v2);
    void _rasterize_scanline(const int x, const int y1, const Color4f c1,
                             const Texcoord2f tex1, const int y2,
                             const Color4f c2, const Texcoord2f tex2);
    void _rasterize_right_triangle(const Vertex &v1, const Vertex &v2,
                                   const Vertex &vr,
                                   const bool no_start = false);
    void _rasterize_left_triangle(const Vertex &v1, const Vertex &v2,
                                  const Vertex &vl,
                                  const bool no_start = false);
    void _rasterize_triangle(const Vertex &v1, const Vertex &v2,
                             const Vertex &v3);

    Texture *_target;
};  // class Renderer

}  // namespace rsr

#endif  // RSRL_RENDERER_H_
